from django.shortcuts import render

# Create your views here.
''' # CHANGELOG
1. 将处理的 脉讯数据 放到这个位置
'''
import logging, copy, json, codecs

from django.views import View
from django.http.request import HttpRequest
from django.http import JsonResponse
from django.utils import timezone
from django.db.models import Q

from .models import MaixunCuZuModel
from .models import MaixunCuZuResModel

with codecs.open(
        filename='idmi_backend/data_configs/maixun_fid_20250717.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    脉讯数据源的 fid
    '''
    FID2INFO = {}
    _ = json.load(fp=fr)
    for key, value in _.items():
        FID2INFO[int(key)] = value

class MaiXunCuZuStorage(View):
    def post(self, request: HttpRequest) -> JsonResponse:
        msg = request.POST.get('msg', '')
        msg = json.loads(msg)
        print('收到数据')
        print(type(msg))
        print(msg)
        print('收到数据')
        maixun_fid = msg.get('maixun_fid', '')
        if maixun_fid in FID2INFO:
            maixun_fid_info = FID2INFO[maixun_fid]
        else:
            logging.error(f'Maixun-cuzu data source : {maixun_fid} does not exists'.format(maixun_fid=maixun_fid))
            return JsonResponse({'status': 200})

        create_time = msg.get('create_time', '')
        info_category = 'maixun_crawled'
        URL = msg.get('URL', '')[:400]
        forum_type_res = msg.get('forum_type_res', '')
        content_type = msg.get('content_type', 'UGC')
        hint_words = msg.get('hint_words', '')
        if isinstance(hint_words, list):
            hint_words = ','.join(hint_words)
        elif isinstance(hint_words, str):
            ...
        else:
            hint_words = ''

        is_comment = msg.get('is_comment', 0)

        target_vehicle_sentence = msg.get('target_vehicle_sentence', '')[:1000]
        target_vehicle_sentence_en = msg.get('target_vehicle_sentence_en', '')[:3000]
        maixun_title = msg.get('maixun_title', '')[:500]
        maixun_text = msg.get('maixun_text', '')[:2000]
        website = msg.get('website', '')[:100]
        maixun_author_location = msg.get('maixun_author_location', '')[:50]
        maixun_domain = msg.get('maixun_domain', '')[:50]

        BodyType = maixun_fid_info['BodyType']
        Brand = maixun_fid_info['Brand']
        BrandIndicator = maixun_fid_info['BrandIndicator']
        FuelType = maixun_fid_info['FuelType']
        Group = maixun_fid_info['Group']
        ModelName = maixun_fid_info['ModelName']
        OEM = maixun_fid_info['OEM']
        Segment = maixun_fid_info['Segment']

        maixun_cuzu = MaixunCuZuModel.objects.create(
            create_time=create_time,
            # info_category=info_category,
            URL=URL,
            forum_type_res=forum_type_res,
            content_type=content_type,
            hint_words=hint_words,
            is_comment=is_comment,
            target_vehicle_sentence=target_vehicle_sentence,
            target_vehicle_sentence_en=target_vehicle_sentence_en,
            maixun_title=maixun_title,
            maixun_text=maixun_text,
            website=website,
            maixun_author_location=maixun_author_location,
            maixun_domain=maixun_domain,
            BodyType=BodyType,
            Brand=Brand,
            BrandIndicator=BrandIndicator,
            FuelType=FuelType,
            Group=Group,
            ModelName=ModelName,
            OEM=OEM,
            Segment=Segment,
            maixun_fid=maixun_fid
        )
        maixun_cuzu_res = msg.get('maixun_cuzu_res', [])

        for cuzu_index in range(len(maixun_cuzu_res)):
            MaixunCuZuResModel.objects.create(
                cuzu=maixun_cuzu,
                cuzu_label_1_int_value=maixun_cuzu_res[cuzu_index]['feel_1_int_value'],
                cuzu_label_2_int_value=maixun_cuzu_res[cuzu_index]['feel_2_int_value'],
                content_list_zh=maixun_cuzu_res[cuzu_index]['content_list_zh'],
                content_list_en=maixun_cuzu_res[cuzu_index]['content_list_en'],
                sentiment=maixun_cuzu_res[cuzu_index]['sentiment'],
            )
        logging.info(f'Maixun-cuzu data source : {maixun_fid} saved'.format(maixun_fid=maixun_fid))
        print('数据存入')
        return JsonResponse({'status': 200})


